package org.linlinjava.litemall.wx.web;

        import static org.linlinjava.litemall.wx.util.WxResponseCode.COMMON_DETAIL_IS_NULL;

        import java.time.LocalDateTime;

        import javax.validation.constraints.NotNull;

        import org.apache.commons.lang3.StringUtils;
        import org.apache.commons.logging.Log;
        import org.apache.commons.logging.LogFactory;
        import org.linlinjava.litemall.core.util.ResponseUtil;
        import org.linlinjava.litemall.core.validator.Order;
        import org.linlinjava.litemall.core.validator.Sort;
        import org.linlinjava.litemall.db.domain.LitemallFormSupply;
        import org.linlinjava.litemall.db.domain.LitemallWantBuy;
        import org.linlinjava.litemall.db.service.SupplyService;
        import org.linlinjava.litemall.db.service.wantBuyService;
        import org.linlinjava.litemall.db.util.SequenceUtils;
        import org.linlinjava.litemall.wx.annotation.LoginUser;
        import org.linlinjava.litemall.wx.service.WxQualificationsService;
        import org.linlinjava.litemall.wx.service.WxSupplyService;
        import org.linlinjava.litemall.wx.vo.QualificationsVo;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.validation.annotation.Validated;
        import org.springframework.web.bind.annotation.GetMapping;
        import org.springframework.web.bind.annotation.PostMapping;
        import org.springframework.web.bind.annotation.RequestBody;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestParam;
        import org.springframework.web.bind.annotation.RestController;

/**
 * 药材供应
 */
@RestController
@RequestMapping("/wx/wantBuy")
@Validated
public class WxWantBuyController {
    private final Log logger = LogFactory.getLog(WxSupplyController.class);

    @Autowired
    private wantBuyService wantBuyService;
    @Autowired
    private WxQualificationsService wxQualificationsService;

    @GetMapping("list")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order,
                       @RequestParam(defaultValue = "") String searchValue) {

        return wantBuyService.querySelective(page, limit, sort, order, searchValue);
    }

    @PostMapping("save")
    public Object save(@LoginUser Integer userId, @RequestBody LitemallWantBuy formSupply) {
        System.out.println(formSupply.toString());
        if (userId == null) {
            return ResponseUtil.unlogin();
        }

        QualificationsVo qualificationsVo = wxQualificationsService.getQualificationsVo(userId);
        if (qualificationsVo == null) {
            return ResponseUtil.fail(COMMON_DETAIL_IS_NULL, "获取用户资质信息异常，请刷新再试");
        }

        formSupply.setShopId(qualificationsVo.getId());

        formSupply.setId(null);
        formSupply.setUserId(userId);
        formSupply.setStatus((byte) 0);
        wantBuyService.add(formSupply);

        return ResponseUtil.ok();
    }

    @GetMapping("detail")
    public Object detail(@NotNull Integer id) {
        System.out.println(id);
        System.out.println(wantBuyService.query(id).toString());
        return wantBuyService.query(id);
    }

    @GetMapping("myList")
    public Object myList(@LoginUser Integer userId, @NotNull Integer type, @RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer limit,
                         @Sort @RequestParam(defaultValue = "add_time") String sort,
                         @Order @RequestParam(defaultValue = "desc") String order,
                         @RequestParam(defaultValue = "") String searchValue) {

        return wantBuyService.myList(userId, type, page, limit, sort, order,searchValue);
    }

    @GetMapping("delete")
    public Object delete(@NotNull Integer id,@LoginUser Integer userId) {
        return wantBuyService.delete(userId,id);
    }

}